package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dto.EmpQueryRequest;
import com.itheima.dto.PageBean;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.pojo.EmpLog;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageBean<Emp> listQueryEmpPage(EmpQueryRequest request) {
        Integer page = request.getPage() <= 0 ? 1 : request.getPage();
        Integer pageSize = request.getPageSize() <= 0 ? 10 : request.getPageSize();
        PageHelper.startPage(page, pageSize);
        List<Emp> empList = empMapper.queryEmpPage(request);
        Page<Emp> empPage = (Page<Emp>) empList;
        return new PageBean<>(empPage.getTotal(), empPage.getResult());
    }

    @Override
    @Transactional
    public void save(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insertEmp(emp);
            Integer id = emp.getId();
//            int i=10/0;
            List<EmpExpr> empExprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(empExprList)) {
                empExprList.stream().forEach(empExpr -> empExpr.setEmpId(id));
                empExprMapper.insertBatch(empExprList);
            }
        } finally {
            empLogService.insertLog(new EmpLog(null, LocalDateTime.now(), emp.toString()));
        }
    }

    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        if (ids==null || ids.size()==0) {
            throw new BusinessException("参数不合法");
        }
        if (!CollectionUtils.isEmpty(ids)) {
            empMapper.deleteByIds(ids);
            empExprMapper.deleteByEmpIds(ids);
        }
    }

    @Override
    public Emp getInfoById(Integer id) {
        return empMapper.getInfoById(id);
    }

    @Override
    @Transactional
    public void update(Emp emp) {
        Integer id = emp.getId();
        emp.setUpdateTime(LocalDateTime.now());
        //修改员工基本信息
        empMapper.updateById(emp);
        //删除员工经历
        empExprMapper.deleteByEmpIds(Arrays.asList(id));
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.stream().forEach(empExpr -> empExpr.setEmpId(id));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public List<Emp> getClazz() {
        List<Emp> empList=empMapper.getClazz();
        return empList;
    }
}
